package org.itboys.weixin.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.weixin.entity.WeixinConfig;
import org.itboys.weixin.entity.WeixinMenu;
import org.itboys.weixin.entity.WeixinMessage;
import org.itboys.weixin.service.WeixinConfigService;
import org.itboys.weixin.service.WeixinMenuService;
import org.itboys.weixin.service.WeixinMessageService;
import org.itboys.weixin.tools.WeixinMenuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

@RestController
@RequestMapping(value = "/weixin/menu")
public class WeixinMenuController extends BaseController {

	@Autowired
	private WeixinMenuService weixinMenuService; 
	@Autowired
	private WeixinMessageService weixinMessageService;
	@Autowired
	private WeixinConfigService weixinConfigService;
	@Autowired
	private ResourceHolder resourceHolder;
	
	@RequestMapping("/vm/{weixinId}")
	public ModelAndView projectMenus(@PathVariable("weixinId") Long weixinId,HttpServletRequest request,Model model){
		model.addAttribute("weixinId",weixinId);
		return new ModelAndView("/weixin/menu/list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		List<WeixinMenu> list = weixinMenuService.list(param);
		for(WeixinMenu wm:list){
			if(wm.getType()==WeixinMenu.TYPE_PARENT){
				param.clear();
				param.put("parentId", Long.valueOf(wm.getId()));
				wm.setChildNum(weixinMenuService.count(param));
			}
		}
		JsonPageUtils.renderJsonPage(list!=null&&list.size()>0?list.size():0L, list, response);
	}
	
	
	@RequestMapping("/getById")
	public ModelAndView getById(HttpServletRequest request,HttpServletResponse response,Model model){
		String id = request.getParameter("id");
		String parentId = request.getParameter("parentId");
		String weixinId=request.getParameter("weixinId");
		String parentName = request.getParameter("parentName");
		model.addAttribute("weixinId", weixinId);
		if(StringUtils.isNotBlank(id)){
			WeixinMenu entity = weixinMenuService.getById(Long.valueOf(id));
			if(entity.getWxMsgId()!=null){
				WeixinMessage weixinMessage=weixinMessageService.getById(entity.getWxMsgId());
				if(null!=weixinMessage){
					entity.setInfoTitle(weixinMessage.getDesc());
				}
			}
			model.addAttribute("entity", entity);
		}
		if(StringUtils.isNotBlank(parentId)){
			model.addAttribute("parentId", parentId);
			model.addAttribute("parentName", parentName);
		}
//		Map<String ,Object> sqlMap=Maps.newHashMap();
//		sqlMap.put("weixinId", weixinId);
//		List<WeixinMessage> wmlist=weixinMessageService.list(sqlMap);
//		model.addAttribute("list", wmlist);
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		return  new ModelAndView("/weixin/menu/input");
	}
	
	@RequestMapping("/save")
	public void insert(HttpServletResponse response, @ModelAttribute WeixinMenu weixinMenu){
		try {
			if(weixinMenu.getId()==0L){
				if(weixinMenu.getType().equals("click")){
					weixinMenu.setValue("MS_"+weixinMenu.getWxMsgId());
				}
				weixinMenuService.save(weixinMenu);
			}else{
				if(weixinMenu.getType().equals("click")){
					weixinMenu.setValue("MS_"+weixinMenu.getWxMsgId());
				}
				weixinMenuService.update(weixinMenu);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinmenu insert error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletResponse response,HttpServletRequest request){
		try{
			Long id =  Long.valueOf(request.getParameter("id"));
			weixinMenuService.delete(id);
			AjaxUtils.renderJson(response, 1);
		}catch (Exception e) {
			logger.error("weixinmenu delete error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
	
	@RequestMapping("/checkvalue")
	public void checkvalue(HttpServletResponse response,HttpServletRequest request){
		try{
			String weixinId=request.getParameter("weixinId");
			String id=request.getParameter("id");
			String value=request.getParameter("value");
			if(weixinMenuService.checkvalue(Long.valueOf(id), Long.valueOf(weixinId), value)){
				AjaxUtils.renderJson(response,1);
			}else{
				AjaxUtils.renderJson(response, 0);
			}
		}catch (Exception e) {
			logger.error("weixinmenu checkvalue error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
	
	@RequestMapping("/doCreateWeixinMenu")
	public void doCreateWeixinMenu(HttpServletResponse response,HttpServletRequest request){
		try{
			Long weixinId=Long.valueOf(request.getParameter("weixinId"));
			WeixinConfig wc=weixinConfigService.getById(Long.valueOf(weixinId));
			Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
			param.put("weixinId", weixinId);
			List<WeixinMenu> list = weixinMenuService.list(param);
			AjaxUtils.renderJson(response,WeixinMenuUtils.doCreateWeixinMenu(wc.getAppId(), wc.getAppSecret(), list));
		}catch (Exception e) {
			logger.error("weixinmenu doCreateWeixinMenu error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
}
